var Cadremember = function() {


    var datalist_url = $('.datalist_url').val();
    var edit_url = $('.edit_url').val();
    var delete_url = $('.delete_url').val();
    var redirect_url = $('.redirect_url').val();
    var save_url = $('.save_url').val();


    var initTable = function() {
        var table = $('#data-table');

        if(table.length > 0){
            table.dataTable({
                "processing": true,
                serverSide: true,// 开启服务器模式
                "ajax": {
                    url: datalist_url,
                },
                "autoWidth": false,
                "columns": [
                    {
                        "data": "avatar",
                        "render": function(data, type, row, meta) {
                            if(data){
                                return '<a href="'+data+'" target="_blank"><img src="'+data+'" style="width: 40px;height: 40px;"/></a>';
                            }else{
                                if(row.gender == 2 || row.gender == '女'){
                                    return '<img src="/public/static/admin/img/default_female.png" alt="" style="width: 40px;height: 40px;" />';
                                }else{
                                    return '<img src="/public/static/admin/img/default_male.png" alt="" style="width: 40px;height: 40px;" />';
                                }
                            }
                        }
                    },
                    {"data": "name"},
                    {
                        "data": "gender",
                        "render": function(data, type, row, meta) {
                            if(row.gender == 1){
                                return '男';
                            }else{
                                return '女';
                            }
                        }
                    },
                    {"data": "mobile"},
                    {"data": "identityNumber"},
                    {"data": "area"},
                    {"data": "organizationName"},
                    {"data": "job"},
                    {
                        //"width": "15%",
                        "data": "null",
                        "render": function(data, type, row, meta) {
                            var html = '';
                            html += '<a href="'+edit_url+'?id='+row.id+'" type="button" class="btn btn-success">编辑</a>';
                            html += '<button type="button" class="btn btn-danger delete-btn">删除</button>';

                            return html;
                        }
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "scrollX":"",
                "destroy": false,
                "stateSave": true,
                "stateSaveParams": function (settings, data) {
                    var search = data.search.search;
                    if(search){
                        var arr = $.parseJSON(search);
                        for(var key in arr){
                            $("#searchForm input[name='"+key+"']").val(arr[key]);
                            $("#searchForm select[name='"+key+"']").val(arr[key]);
                        }
                    }
                },
                "order": [
                    [6, "asc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0, 1, 2, 3, 4, 5, 7, 8]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    //var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                    var city = $('input[name=city]').val();
                    if(city){
                        //$("#city").find("option:contains('"+city+"')").attr("selected", true);
                        get_xian($("#city"));
                    }
                }
            });
        }

        // 筛选
        $('#doSearch').on('click', function(event){
            var param = $('#searchForm').serializeObject();

            tableSearch(table, param);
        });

        // 删除
        table.on('click', '.delete-btn', function(event) {
            // 操作行对象
            var dataArr = table.DataTable().rows($(this).parents("tr")).data();
            var id = dataArr[0].id;
            var _this = $(this);

            var text = '是否要删除这条数据？';
            var title = '请确认';
            var confirmBtn = '确定';
            var cancelBtn = '取消';
            event.preventDefault();

            layer.confirm(
                text,
                {
                    title: title,
                    btn: [confirmBtn, cancelBtn]
                },
                function(index){
                    layer.close(index);
                    var url = delete_url;
                    var data = {'id':id};
                    $.ajax({
                        url: url,
                        type: 'POST',
                        data: data,
                        dataType : 'json',
                        success: function (data) {
                            layer.msg(data.msg);
                            if(data.code == 1){
                                _this.parents('tr').remove();
                            }
                        }
                    });
                }
            );
        });
    };

    var tableSearch = function(table, params) {

        table.DataTable().search(JSON.stringify(params)).draw();
    };


    // 表单提交
    var submitForm = function(btnObj) {
        var form = document.getElementById("ajax-form");
        var _this = btnObj;

        if(form.memberId.value.trim() == '') {
            layer.msg('请选择人员');
            return;
        }
        if(form.organizationId.value.trim() == '') {
            layer.msg('请选择组织');
            return;
        }
        if(form.jobId.value.trim() == '') {
            layer.msg('请选择职务');
            return;
        }

        var _data = $(form).serializeObject();
        _this.prop("disabled", true);

        $.ajax({
            url : save_url,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify(_data),
        }).done(function(data) {
            _this.prop("disabled", false);
            if (data.code == 1) {
                layer.msg('保存成功');
                window.location.href = data.url;
            } else {
                layer.msg(data.msg);
            }
        });
    };


    return {
        init: function() {
            initTable();

            this.onEvent();
        },

        onEvent: function() {


            // 选择组织
            $('.select-organization').on('click', function(){
                var content = $('.organizations_url').val();
                var index = layer.open({
                    title : "选择组织",
                    type : 2,
                    area : ["900px", "600px"],
                    content : content,
                    success : function(layero, index){
                        //
                    }
                });
            });

            // 表单提交
            $('#form-submit').on('click', function(){
                var form = this.form;
                var _this = $(this);

                submitForm(_this);
            });
        }

    };

}();

jQuery(document).ready(function() {
    Cadremember.init();

});
